//
// Created by hjie on 23-7-6.
//
#include <string>
#include <cstring>
#include <vector>

void ProducerData(std::vector<int> & data, int number)
{
    srand(time(NULL));
    for (int index = 0; index < number; index++)
    {
        data.push_back(rand()%10);
    }
    printf("%s : ", __func__ );
    for (auto & item : data)
    {
        printf("%d ", item);
    }
    printf("\n");
}

void stdMethod()
{
    int number = 30;
    std::vector<int> number_data;
    ProducerData(number_data, number);
    int * array = &(*number_data.begin());

    int slow_index = 0;
    for (int fast_index = 0; fast_index < number; fast_index++)
    {
        if (array[fast_index] != 0)
        {
            array[slow_index] = array[fast_index];
            slow_index++;
        }
    }
    for (int index = slow_index; index < number; index++)
    {
        array[index] = 0;
    }
    printf("result  data : ");
    for (auto & item : number_data)
    {
        printf("%d ", item);
    }
    printf("\n");
}


void MoveZero()
{
    // take zero index flag (if the array only one zero element) and to swap the data
    // compute zero count and loop swap (by myself)
    int number = 30;
    std::vector<int> number_data;
    ProducerData(number_data, number);
    int * array = &(*number_data.begin());

    int zero_count = 0;
    for (int index = 0; index < number; index++)
    {
        if (array[index] == 0)
        {
            zero_count++;
        }
    }
    int begin_index = 0;
    for (int i = 0; i < zero_count; i++)
    {
        int zero_index = -1;
        for (int index = begin_index; index < number; index++)
        {
            if (array[index] == 0)
            {
                zero_index = index;
                if (begin_index == 0)
                {
                    begin_index = zero_index;
                }
            }
            else if (zero_index != -1)
            {
                int tmp = array[zero_index];
                array[zero_index] = array[index];
                array[index] = tmp;
                zero_index = index;
            }
        }
    }

    printf("result  data : ");
    for (auto & item : number_data)
    {
        printf("%d ", item);
    }
    printf("\n");
}


int main(int argc, const char * argv[])
{
    for (int index = 0; index < 5; index++)
    {
        stdMethod();
        MoveZero();
        printf("\n");
    }
    return 0;
}